package com.example.jenkinstest.controller.html;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;

public class WebD {
    private static String projectPath = System.getProperty("user.dir");
    private static String jsPath = projectPath + File.separator + "huicong.js";
    private static String exePath = "D:\\phantomjs-2.5.0-beta-windows\\bin"+ File.separator
            + "phantomjs.exe";

    //可以
    public static void main(String[] args) {
        WebDriver webDriver =null;
        try {
            webDriver =getPhantomJs();
            webDriver.get("https://item.jd.com/100020318814.html");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
//            SleepUtil.sleep(Constant.SEC_5);
//            PhantomJsUtil.screenshot(webDriver);
            WebDriverWait wait = new WebDriverWait(webDriver, 10);
            wait.until(ExpectedConditions.presenceOfElementLocated(By.id("p-ad")));//开始打开网页，等待输入元素出现
            Document document = Jsoup.parse(webDriver.getPageSource());
            System.out.println(document);

//TODO 　剩下页面的获取就按照Jsoup获取方式来做

        } finally {
            if (webDriver != null) {
                webDriver.quit();
            }
        }
    }


    public static WebDriver getPhantomJs() {
        String osname = System.getProperties().getProperty("os.name");
        if (osname.equals("Linux")) {//判断系统的环境win or Linux
            System.setProperty("phantomjs.binary.path", "/usr/bin/phantomjs");
        } else {
            System.setProperty("phantomjs.binary.path",exePath);//设置PhantomJs访问路径
        }
        DesiredCapabilities desiredCapabilities = DesiredCapabilities.phantomjs();
//设置参数
        desiredCapabilities.setCapability("phantomjs.page.settings.userAgent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
        desiredCapabilities.setCapability("phantomjs.page.customHeaders.User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0");
//        if (false) {//是否使用代理
//            org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
//            proxy.setProxyType(org.openqa.selenium.Proxy.ProxyType.MANUAL);
//            proxy.setAutodetect(false);
//            String proxyStr = "";
//            do {
//                proxyStr = ProxyUtil.getProxy();//自定义函数，返回代理ip及端口
//            } while (proxyStr.length() == 0);
//            proxy.setHttpProxy(proxyStr);
//            desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
//        }
        return new PhantomJSDriver(desiredCapabilities);
    }

}
